home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-04 | 6.5 KB | 299 lines | [TEXT/CWIE] |
- // mac_main.c
- // 28May1997 e -- derived from uio.c for mosml
-
- #include <Dialogs.h>
- #include <Events.h>
- #include <Files.h>
- #include <Stdlib.h>
- #include <Types.h>
- #include <Resources.h>
- #include <console.h>
- #include "ui.h"
-
- #if defined( THINK_C ) || defined ( __MWERKS__ )
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
- #include "os_mac_eventchk.h"
- #ifdef THINK_C
- #include <fcntl.h>
- #include <stat.h>
- #else
- #include <unix.h>
- #endif
- #endif
-
- #include <Gestalt.h>
-
- extern Boolean gMadeAppFSS;
- extern char gReceivedAE;
- extern FSSpec appFSS;
- extern int sub_main (int argc, char *argv []);
- extern unsigned char *console_title;
- extern char *image_name;
- #ifdef THINK_C
- extern WindowPeek cflush(FILE *fp);
- #endif
-
- // get_wd_name must copy pstr to a malloc'd C string
- // if relative path name, construct full path name
- // bashing fname is permitted
-
- char *get_wd_name( Str255 fname )
- {
- int len = fname[0];
- char *p, *res;
- if( len == 0 )
- { // null string
- p = (char *)&fname[0];
- }
- else
- { // see if 1st char ':' or has no ':'
- fname[len+1] = 0; // make it a bi-string
- if( fname[1] == ':' )
- {
- fname[1] = fname[0] -1;
- fname = &fname[1];
- goto make_full_path;
- }
- else if( strchr( (char *)&fname[2], ':' ) == NULL )
- { // make relative
- char path[512];
- make_full_path:
- len = getfullpath( appFSS.vRefNum, appFSS.parID, fname, path, 511, 0 );
- p = path;
- }
- else
- { // it's absolute
- p = (char *)&fname[1];
- }
- }
- res = malloc( len + 1 );
- if( res != 0 ) memcpy( res, p, len + 1 );
- return res;
- }
-
- // puts, but quotify string suitably for ecommand
-
- static puts_quotify( char *str )
- {
- // fputs( str, stdout );
- char buf[512];
- int qsp = 0;
- int qsq = 0;
- int qdq = 0;
- int qbs = 0;
- int len = 0;
- char c, *s = str;
- // count funny chars
- while( (c = *s++) != 0 )
- {
- if( c == ' ' ) qsp++;
- else if( c == '"' ) qdq++;
- else if( c == '\'' ) qsq++;
- else if( c == '\\' ) qbs++;
- len++;
- }
- if( qsp + qsq + qdq + qbs == 0 || len > 256 )
- { // no funny chars
- s = str; // no problem
- }
- else
- { // maybe quotify each funny char
- char q, *t = buf;
- if( qsp + qsq + qdq == 0 ) q = 0; // no outer quotes if only '\\'
- else if( qdq <= qsq ) q = '"'; // else outer quote with '"' or '\''
- else q = '\''; // whichever is used less often
- if( q != 0 ) *t++ = q;
- s = str;
- while( (c = *s++) != 0 )
- {
- if( c == q || c == '\\' ) *t++ = '\\';
- *t++ = c;
- }
- if( q != 0 ) *t++ = q;
- *t = 0;
- s = buf;
- }
- fputs( s, stdout );
- fputs( " ", stdout );
- }
-
- #ifdef __MC68K__
- #if __MC68K__
- static void die( unsigned char *txt )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- InitCursor();
- ParamText( txt, "\p", "\p", "\p" );
- StopAlert( ok_alertID, 0L );
- ExitToShell();
- }
- #endif
- #endif
-
-
- static long probe_image( char *name ) // returns length or -1
- {
- long len = -1;
- int fd = open( name, O_RDONLY | O_BINARY );
- if (fd != -1)
- {
- len = lseek( fd, 0, SEEK_END );
- close( fd );
- }
- return len;
- }
-
- #define MIN_IMAGE_SIZE 1200
-
- main ()
- {
- int argc, fd;
- char **argv;
- char *args[6];
- KeyMap kMap;
- long len;
- Str255 pstr;
-
- if ( GetApplLimit() > ( LMGetCurStackBase() - 40000 ) )
- { SetApplLimit ( LMGetCurStackBase() - 40000 );
- }
-
- #ifdef __MC68K__
- #if __MC68K__
- #if __MC68020__
- if( /* NG on Sys 7.1 and others? -- 08Oct96 e
- Gestalt( gestaltNativeCPUtype, &len ) || len < gestaltCPU68020
- */
- Gestalt( gestaltProcessorType, &len ) || len < gestalt68020
- )
- { die( "\pAn M68020 or better processor is required to run this application!" );
- }
- #endif
- #if __MC68881__
- if( Gestalt( gestaltFPUType, &len ) || len == gestaltNoFPU )
- { die( "\pAn FPU (or M64040) is required to run this application!\
- Try using the SANE version." );
- }
- #endif
- #endif
- #endif
-
- // first, see if we were launched with an image document
- // to do that we need to init MacOS which is done by the console
- GetIndString(pstr, 357, 5); /* console title */
- if ( pstr[0] == 0 )
- console_title = "\pMosml Console";
- else
- console_title = pstr;
- #ifdef THINK_C
- cflush(stdin);
- #else
- puts(" ");
- fflush(stdout);
- #endif
- for( fd = 0; fd < 20 && gReceivedAE == 0; fd++ )
- os_event_check(); // let any open apple events in
-
- if( gReceivedAE < 0 )
- goto handle_AE_cmd_line; // bail if we were launched with command line
-
- // see if ProcessManager call set up appFSS
- if( ! gMadeAppFSS )
- { // if not, try to fill in the blanks
- unsigned char *CurApName = LMGetCurApName();
- short wd;
- GetVol( NULL, &wd );
- appFSS.vRefNum = 0;
- GetWDInfo( wd, &appFSS.vRefNum, &appFSS.parID, &len );
- if( (wd = CurApName[0]) < 64 )
- memcpy( appFSS.name, CurApName, wd );
- }
-
- /* arg 0 */
- len = (image_name == NULL) ? 0 : probe_image( image_name );
- if( len >= MIN_IMAGE_SIZE )
- {
- args[0] = image_name;
- }
- else
- {
- GetIndString(pstr, 357, 1); /* image name */
- args[0] = get_wd_name( pstr );
- len = probe_image( args[0] );
- }
- if( len >= 0 )
- {
- GetKeys( kMap );
- // fd = ((len < 12000) || (kMap[1] & 4)) ? -1 : 0; -- option key
- fd = ((len < MIN_IMAGE_SIZE) || (kMap[1] & 32768L)) ? -1 : 0;
- } /* probe image, insure big enough, command key for command line */
- else fd = -1;
-
- /* arg 1 */
- args[1] = "-stdlib";
-
- /* arg 2 */
- GetIndString(pstr, 357, 2); /* stdlib directory name */
- args[2] = get_wd_name( pstr );
-
- argc = 3;
-
- /* optional arg 3 */
- GetIndString(pstr, 357, 3); /* options, e.g., -P */
- if ( pstr[0] != 0 )
- {
- args[3] = malloc( pstr[0] );
- if( args[3] != NULL )
- { argc = 4;
- p_to_c( pstr, args[3] );
- /* optional arg 4 */
- GetIndString(pstr, 357, 4); /* options, e.g., {full,none,etc.} */
- if ( pstr[0] != 0 )
- {
- args[4] = malloc( pstr[0] );
- if( args[4] != NULL )
- { argc = 5;
- p_to_c( pstr, args[4] );
- }
- }
- }
- }
-
- if ( fd != -1 )
- {
- argv = args;
- argv[argc] = NULL;
- }
- else
- {
- if ( 1 /* kMap[1] & 1 */ ) // shift key
- {
- puts_quotify( args[0] );
- fputs( args[1], stdout );
- fputs( " ", stdout );
- puts_quotify( args[2] );
- fputs( args[3], stdout );
- fputs( " ", stdout );
- puts( args[4] );
- }
- handle_AE_cmd_line:
- argc = ecommand( (unsigned char ***)&argv );
- }
-
- if( argc > 0 ) // ecommand can do this now -- 13May97 e
- {
- init_timers();
- caml_main( argc, argv );
- }
- return 0;
- }
-
- // end of mac_main.c
-